﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using CheckLift.Linq;
using System.Windows.Data;
using System.Windows.Input;
using CheckLift.Common.Intf;

namespace CheckLift.ViewModels
{
    public class ReportViewModel : INotifyPropertyChanged
    {
        private ObservableCollection<IReport> reportsByCompany;
        private ObservableCollection<IReport> reportsByService;
        private Guid activeUserId = Guid.Empty;
        private ObservableCollection<ICompany> companies;
        private ObservableCollection<IServiceCompany> serviceCompanies;


        public ReportViewModel(Guid userId)
        {
            activeUserId = userId;            
        }

        public ObservableCollection<ICompany> Companies
        {
            get
            {
                if (companies == null)
                {
                    //if (Reports == null)
                    //    RefreshReportViewModel();
                    companies = new CompanyData().GetCompaniesByUserId(activeUserId, Guid.Empty);
                }
                return companies;
            }
        }

        public ObservableCollection<ILift> Lifts
        {
            get
            {
                return new LiftData().GetAllLiftsByUserId(activeUserId);
            }
        }

        public ObservableCollection<IServiceCompany> ServiceCompanies
        {
            get
            {
                if (serviceCompanies == null)
                {
                    //if (Reports == null)
                    //    RefreshReportViewModel();
                    serviceCompanies = new ServiceCompanyData().GetServiceCompanies();
                }
                return serviceCompanies;
            }
        }

        public ObservableCollection<IReport> Reports 
        {
            get 
            {
                return reportsByCompany ?? new ReportDataImpl().GetReportsByUser(activeUserId);                 
            }
            private set
            {
                if (object.ReferenceEquals(reportsByCompany, value) != true)
                {
                    reportsByCompany = value;                   
                }                
            }
        }

        public ObservableCollection<IReport> ReportsByService
        {
            get
            {
                return reportsByService ?? new ReportDataImpl().GetReportsByUser(activeUserId);
            }
            private set
            {
                if (object.ReferenceEquals(reportsByService, value) != true)
                {
                    reportsByService = value;
                }
            }
        }

        public ObservableCollection<Customer> GetAllCustomersByUser()
        {
            return new CustomerData().GetAllCustomers(activeUserId);
        }

        Guid activeCompanyId = Guid.Empty;

        public Guid CompanyChange
        {
            get { return activeCompanyId; }

            set
            {
                activeCompanyId = value;
                OnPropertyChanged("CompanyChange");
            }
        }

        public int ServiceCompanyChange
        {
            get { return serviceCompanyId; }

            set
            {
                serviceCompanyId = value;
            }
        }

        private int tabChoosed = 0;
        public int TabChanged
        {
            get
            {
                return tabChoosed;
            }
            set
            {
                if (value < 0)
                    value = 0;
                tabChoosed = value;                
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private int serviceCompanyId;
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        private void RefreshReportViewModel()
        {
            Reports = new ReportDataImpl().GetReportsByUser(activeUserId);
        }
    }
}
